﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using sccmclictr.automation;
using Microsoft.SystemCenter.Orchestrator.Integration;
using System.Management;

namespace OpalisAddOn.TriggerActions
{
    [Activity("Trigger Agent Action", Description="Trigger SCCM Agent Actions", ShowFilters=false)]
    public class TriggerAgentAction : IActivity
    {
        private String targetComputer;
        private String userName;
        private string password;

        public void Design(IActivityDesigner designer)
        {
            designer.AddInput("TargetComputer").WithComputerBrowser();
            designer.AddInput("Action").WithListBrowser(new object[] 
            { "HW Inventory", "SW Inventory", "Heartbeat", "Collect Files", "Machine Policy", "User Policy", "MSI Source Update", "DCM Scan", "Metering Scan",
                "OOB Discovery", "Certificate Maintenance", "Location Refresh", "Refresh MP", "Timeout Requests", "Reset Policy", "Delete orphaned cached packages", "Delete all cached packages", "Delete cached updates", "Update Evaluation", "Update Scan"  });
            designer.AddInput("UserName").NotRequired();
            designer.AddInput("Password").NotRequired().PasswordProtect();
            designer.AddInput("Full Inventory").WithBooleanBrowser().WithDefaultValue(false).NotRequired();
            designer.AddOutput("Action").AsString().WithDescription("The last triggered action");
        }

        public void Execute(IActivityRequest request, IActivityResponse response)
        {
            sccmclictr.automation.SCCMAgent oClient;
            targetComputer = request.Inputs["TargetComputer"].AsString();
            userName = request.Inputs["UserName"].AsString();
            password = request.Inputs["Password"].AsString();

            string action = request.Inputs["Action"].AsString();
            bool bFull = false;

            try
            {
                bFull = request.Inputs["Full Inventory"].AsBoolean();
            }
            catch { }

            if (string.IsNullOrEmpty(userName))
            {
                oClient = new SCCMAgent(targetComputer);
            }
            else
            {
                oClient = new SCCMAgent(targetComputer, userName, password);
            }

            switch (action.ToLower())
            {
                case "hw inventory":
                    oClient.Client.AgentActions.HardwareInventory(bFull);
                    break;
                case "sw inventory":
                    oClient.Client.AgentActions.SoftwareInventory(bFull);
                    break;
                case "heartbeat":
                    oClient.Client.AgentActions.DataDiscovery(false);
                    break;
                case "collect files":
                    oClient.Client.AgentActions.FileCollection();
                    break;
                case "machine policy":
                    oClient.Client.AgentActions.RequestMachinePolicyAssignments();
                    break;
                case "user policy":
                    oClient.Client.AgentActions.RequestUserAssignments();
                    break;
                case "msi source update":
                    oClient.Client.AgentActions.MSISourceListUpdate();
                    break;
                case "dcm scan":
                    oClient.Client.AgentActions.DCMPolicy();
                    break;
                case "metering scan":
                    oClient.Client.AgentActions.SWMeteringUsageReport();
                    break;
                case "oob discovery":
                    oClient.Client.AgentActions.AMTProvisionCycle();
                    break;
                case "certificate maintenance":
                    oClient.Client.AgentActions.CertificateMaintenanceCycle();
                    break;
                case "location refresh":
                    oClient.Client.AgentActions.RefreshLocationServicesTask();
                    break;
                case "refresh mp":
                    oClient.Client.AgentActions.RefreshDefaultMPTask();
                    break;
                case "timeout requests":
                    oClient.Client.AgentActions.TimeoutLocationServicesTask();
                    break;
                case "reset policy":
                    oClient.Client.AgentActions.ResetPolicy(true);
                    break;
                case "delete orphaned cached packages":
                    oClient.Client.SWCache.CleanupOrphanedCacheItems();
                    break;
                case "delete all cached package":
                    oClient.Client.SWCache.CleanupOrphanedCacheItems();
                    break;
                case "delete cached updates":
                    oClient.Client.SWCache.CleanupOrphanedCacheItems();
                    break;
                case "update evaluation":
                    oClient.Client.AgentActions.SoftwareUpdatesAgentAssignmentEvaluationCycle();
                    break;
                case "update scan":
                    oClient.Client.AgentActions.ForceUpdateScan();;
                    break;

            }

            oClient.disconnect();
            response.Publish("Action", action);

        }
    }

}
